package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.ums.entity.UmsBrowse;
import org.jeecg.modules.job.ums.service.IUmsBrowseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *浏览记录模块管理
 */
@RestController
@RequestMapping("/api/browse")
@Api(tags="移动端浏览记录管理模块")
@Slf4j
public class BrowseController {

    @Autowired
    private IUmsBrowseService browseService;

    /**
     * 查询浏览列表
     */
    @RequestMapping(value = "/getBrowseList",method = RequestMethod.GET)
    @ApiOperation(value = "查询浏览列表", notes = "查询浏览列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public Result<Object> getBrowseList(UmsBrowse param,
                                        @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                        @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
    ) {
        try{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (param.getBrowseType()==0){//用户
                param.setMemberId(user.getId());
            }else{//企业
                param.setComMemberId(user.getId());
            }
            IPage<UmsBrowse> pageInfo= browseService.findPageBySpec(new Page<>(pageNo,pageSize),param);
            return Result.ok(pageInfo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询看过我列表
     */
    @RequestMapping(value = "/getVisitList",method = RequestMethod.GET)
    @ApiOperation(value = "查询看过我列表", notes = "查询看过我列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public Result<Object> getVisitList(UmsBrowse param,
                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
    ) {
        try{
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (param.getBrowseType()==0){//企业端
                param.setComMemberId(user.getId());
            }else{//用户端
                param.setMemberId(user.getId());
            }
            IPage<UmsBrowse> pageInfo= browseService.findPageBySpec2(new Page<>(pageNo,pageSize),param);
            return Result.ok(pageInfo);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    /**
     * 查询浏览详情
     */
    @RequestMapping(value = "/getBrowseDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询浏览详情", notes = "查询浏览详情")
    public Result<Object> getBrowseDetail(String id) {
        UmsBrowse result=browseService.getById(id);
        return Result.ok(result);
    }

    /**
     * 添加/更新浏览
     */
    @RequestMapping(value = "/addOrUpdateBrowse",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新浏览", notes = "添加/更新浏览")
    public Result<Object> addOrUpdateBrowse(@RequestBody UmsBrowse param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (param.getBrowseType()==1){//企业端浏览记录
                param.setComMemberId(user.getId());
            }else{
                param.setMemberId(user.getId());
            }
            browseService.update(param);
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除简历浏览
     */
    @RequestMapping(value = "/deleteBrowse",method = RequestMethod.POST)
    @ApiOperation(value = "删除浏览", notes = "删除浏览")
    public Result<Object> deleteBrowse(@RequestBody UmsBrowse param) {
        try {
            browseService.removeById(param.getId());
            return Result.OK();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}



